package com.dbpms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dbpms.mapper.ChamberMapper;
import com.dbpms.mapper.StudentManageMapper;
import com.dbpms.pojo.ChamberInfo;
import com.dbpms.pojo.Chamberfloorinfo;
import com.dbpms.pojo.Chambertypeinfo;
import com.dbpms.pojo.StudentInfo;
import com.dbpms.service.IChamberService;
import com.dbpms.until.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/chamber")
public class ChamberController {

    @Autowired
    private IChamberService iChamberService;
    @Resource
    private ChamberMapper mapper;
    @Resource
    private StudentManageMapper studentManageMapper;
    /**
     * TODO 前往查看寝室页面  有点小问题：条件查询后，数量大于6条，选择第二页重新查询了
     * @param page_no : 当前页
     * @param page_size : 每页存在数
     * @param Chamber_floor_name_select : 条件查询 - 选中了哪寝室楼id
     * @param Chamber_floor_number_select : 条件查询 - 选中了哪楼层
     * @param model
     * @return
     */
    @GetMapping("/toShowInfo")
    public String toShowInfo(@RequestParam(name ="page_no",required = false,defaultValue = "1")int page_no,
                             @RequestParam(name ="page_size",required = false,defaultValue = "5")int page_size,
                             @RequestParam(name ="Chamber_floor_name_select",required = false,defaultValue = "0")int Chamber_floor_name_select,
                             @RequestParam(name ="Chamber_floor_number_select",required = false,defaultValue = "0")int Chamber_floor_number_select,
                             Model model) {

        long total = 1; // 总条数
        long pages = 1; // 总页数
        List<ChamberInfo> chamberInfos; // 寝室数据
        List<Chamberfloorinfo> chamberFloorInfos; // 寝室楼数据
        QueryWrapper<ChamberInfo> wrapper = new QueryWrapper<ChamberInfo>();

        // 获取寝室数据 - 分页查询
        Page<ChamberInfo> page = iChamberService.page(Page.of(page_no,page_size));
        total = page.getTotal();
        pages = page.getPages();

        // 获取全部寝室楼数据
        chamberFloorInfos = iChamberService.queryAllChtFloorInfo();

        // 没有进行条件查询
        if (Chamber_floor_name_select == 0 && Chamber_floor_number_select == 0){
            wrapper.eq("1",1); // ew不能为null
        }else{
            // 进行了条件查询
            wrapper.eq(Chamber_floor_name_select != 0,"ci.chamber_floor_id",Chamber_floor_name_select) // 寝室楼id
                    .eq(Chamber_floor_number_select != 0,"ci.chamber_floor",Chamber_floor_number_select); // 选中了哪楼层
            total = mapper.count(wrapper);
            pages = (int) Math.ceil((double)total /page_size);
        }
        // 1.2 获取寝室数据
        chamberInfos = mapper.queryAllChtInfo(page,wrapper);

        model.addAttribute("chamberInfo",chamberInfos); // 寝室数据
        model.addAttribute("chamberfloorinfos",chamberFloorInfos);  // 寝室楼数据
        model.addAttribute("query_items",chamberInfos.size());  // 分页查出了多少条
        model.addAttribute("total",total);  // 总条数
        model.addAttribute("pages",pages);  // 总页数
        model.addAttribute("page_no",page_no);  // 当前页
        model.addAttribute("page_size",page_size);  // 每页存在数


        model.addAttribute("Chamber_floor_name_select",Chamber_floor_name_select);  // 条件查询 - 选中了哪寝室楼id
        model.addAttribute("Chamber_floor_number_select",Chamber_floor_number_select);  // 条件查询 - 选中了哪楼层
        return "chamber/showChamberInfo";
    }
    /**
     * 执行 - 删除寝室信息
     * @param chamber_id ： 寝室id
     * @param model
     * @return
     */
    @GetMapping("/delete")
    public String deleteStudentInfo(@RequestParam("chamber_id") int chamber_id,
                                    Model model) {
        System.out.println(chamber_id);
        boolean pd;
        try {
            pd = iChamberService.deleteChamberInfo(chamber_id);
        }catch (Exception E){
            pd = false;
        }
        if (pd){
            model.addAttribute(Constants.MESSAGE,"删除成功");
        }else{
            model.addAttribute(Constants.MESSAGE,"删除失败，该寝室还存在其他学生");
        }
        return "chamber/showChamberInfo";
    }

    /**
     * 前往添加寝室信息页面
     * @return
     */
    @GetMapping("/toAddInfo")
    public String toAddInfo(Model model) {

        // 获取寝室楼信息
        List<Chamberfloorinfo>  floorInfos = iChamberService.queryAllChtFloorInfo();
        // 获取寝室类型信息
        List<Chambertypeinfo> typeInfo = iChamberService.queryChtTypeInfo();

        model.addAttribute("chamberfloorinfos",floorInfos);
        model.addAttribute("chambertypeinfos",typeInfo);
        return "chamber/addChamberInfo";
    }

    /**
     * 执行 - 添加寝室信息
     * @param chamberInfo
     * @param model
     * @return
     */
    @PostMapping("/addInfo")
    public String addChamberInfo(ChamberInfo chamberInfo,Model model){
        System.out.println(chamberInfo.toString());
        boolean pd;
        try {
            pd = iChamberService.addChamberInfo(chamberInfo);
        }catch (Exception E){
            pd = false;
        }
        if (pd){
            model.addAttribute(Constants.MESSAGE,"添加成功");
        }else{
            model.addAttribute(Constants.MESSAGE,"添加失败，寝室号重复！！");
        }
        return "chamber/addChamberInfo";
    }
    /**
     * 前往修改寝室信息页面
     * @param chamber_id ：寝室id
     * @param model
     * @return
     */
    @GetMapping("/toUpdateInfo")
    public String toUpdateInfo(@RequestParam("chamber_id") int chamber_id,
                               Model model) {

        // 根据寝室id - 获取寝室信息
        QueryWrapper<ChamberInfo> wrapper = new QueryWrapper<ChamberInfo>()
                .eq("chamber_id",chamber_id);
        ChamberInfo chamberInfo = mapper.queryAllChtInfo(null,wrapper).get(0);
        // 获取寝室楼信息
        List<Chamberfloorinfo>  floorInfos = iChamberService.queryAllChtFloorInfo();
        // 获取寝室类型信息
        List<Chambertypeinfo> typeInfo = iChamberService.queryChtTypeInfo();

        model.addAttribute("chamberfloorinfos",floorInfos);
        model.addAttribute("chambertypeinfos",typeInfo);

        model.addAttribute("chamberInfo",chamberInfo);

        return "chamber/updateChamberInfo";
    }

    /**
     * 执行 - 修改寝室信息
     * @param chamberInfo
     * @param model
     * @return
     */
    @PostMapping("/updateInfo")
    public String updateInfo(ChamberInfo chamberInfo,Model model) {


        System.out.println(chamberInfo);
        boolean pd;
        try {
            pd = iChamberService.updateChamberInfo(chamberInfo);
        }catch (Exception E){
            pd = false;
        }
        if (pd){
            model.addAttribute(Constants.MESSAGE,"修改成功");
        }else{
            model.addAttribute(Constants.MESSAGE,"修改失败，寝室号重复！！");
        }

        return "chamber/updateChamberInfo";
    }
    /**
     * 前往查看寝室学生页面
     * @param chamber_id ：寝室id
     * @param model
     * @return
     */
    @GetMapping("/toShowChamberStuInfo")
    public String toShowChamberStuInfo(@RequestParam("chamber_id") int chamber_id,
                               Model model) {
        QueryWrapper<StudentInfo> wrapper = new QueryWrapper<StudentInfo>()
                .eq("st.student_chamber_id",chamber_id);

        List<StudentInfo> list = studentManageMapper.queryStdInfo_Page(null,wrapper);

        model.addAttribute("ChamberNum_Stu_List",list);
        model.addAttribute("ChamberNum_Stu_row",(int)Math.ceil((double) list.size()/3.0));

        return "chamber/queryChamberInStuInfo";
    }
}
